using System.IO;
using System.Text;
using System.Web;
using Newtonsoft.Json;

namespace OpenWaves.ActivityFeed.Web.Handler
{
    public class JsonResult : IActionResult
    {
        private readonly object Value;

        public JsonResult(object value)
        {
            this.Value = value;
        }

        public void Execute(HttpResponseBase response)
        {
            response.BufferOutput = false;
            response.ContentType = "application/json";
            response.ContentEncoding = Encoding.UTF8;

            var serializer = JsonSerializer.Create(JsonNetSerializerSettings.Current);
            using (var streamWriter = new StreamWriter(new UnclosableStream(response.OutputStream), Encoding.UTF8))
            {
                serializer.Serialize(streamWriter, this.Value);
            }

            response.Flush();
            response.End();
        }
    }
}